//
//  FKViewController.m
//  Link
//
//  Created by yeeku on 13-7-16.
//  Copyright (c) 2013年 crazyit.org. All rights reserved.
//

#import "FKViewController.h"
#import "FKGameView.h"
#import "Constants.h"
#import "FKPiece.h"

@interface FKViewController ()
// 游戏界面类

@property (nonatomic, strong)FKGameView *gameView;
// 游戏剩余时间
@property (nonatomic, assign)NSInteger leftTime;
// 定时器
@property (nonatomic, strong)NSTimer *timer;
@property (nonatomic, assign)BOOL isPlaying;

@property (nonatomic, strong)UIAlertView *lostAlert;

@property (nonatomic, strong)UIAlertView *successAlert;

@end

@implementation FKViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
    
	[UIApplication sharedApplication].statusBarHidden = YES;
	
	// 使用room.jpg作为游戏背景图片
	UIColor *bgColor = [UIColor colorWithPatternImage:
		[UIImage imageNamed:@"room.jpg"]];
	self.view.backgroundColor = bgColor;
		// 创建FKGameView控件
	_gameView = [[FKGameView alloc] initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight - 64)];
	// 创建FKGameService对象
	_gameView.gameService = [[FKGameService alloc] init];
	_gameView.delegate = self;
	_gameView.backgroundColor = [UIColor clearColor];
	[self.view addSubview:_gameView];
    
    self.startBn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.startBn.frame = CGRectMake(0, kScreenHeight - 100 - 20, kScreenWidth / 2, 50);
    
    // 为startBn按钮设置图片
    [self.startBn setBackgroundImage:[UIImage imageNamed:@"start.png"]
                            forState:UIControlStateNormal];
    [self.startBn setBackgroundImage:[UIImage imageNamed:@"start_down.png"]
                            forState:UIControlStateHighlighted];
    // 为startBn的Touch Up Inside事件绑定事件处理方法
    [self.startBn addTarget:self action:@selector(startGame)
           forControlEvents:UIControlEventTouchUpInside];

    
    [_gameView addSubview:self.startBn];
    
    self.timeText = [[UILabel alloc]initWithFrame:CGRectMake(kScreenWidth / 2, self.view.bottom - 100 - 20, kScreenWidth / 2, 50)];
    
    self.timeText.textColor = [UIColor colorWithRed:1 green:1
                                               blue: 9/15 alpha:1];
    
    [_gameView addSubview:self.timeText];
    
    
	// 初始化游戏失败的对话框
	_lostAlert = [[UIAlertView alloc] initWithTitle:@"失败！"
		message:@"游戏失败！重新开始？"delegate:self
		cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
	// 初始化游戏胜利的对话框
	_successAlert = [[UIAlertView alloc] initWithTitle:@"胜利！"
		message:@"游戏胜利！重新开始？"delegate:self
		cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
}
- (void) startGame
{
	// 如果之前的timer还未取消，取消timer
	if (_timer != nil)
	{
		[_timer invalidate];
	}
	// 重新设置游戏时间
	_leftTime = DEFAULT_TIME;
	// 开始新的游戏游戏
	[_gameView startGame];
	_isPlaying = YES;
	_timer = [NSTimer scheduledTimerWithTimeInterval:1
		target:self selector:@selector(refreshView) userInfo:nil repeats:YES];
	// 将选中方块设为nil。
	_gameView.selectedPiece = nil;
}
- (void) refreshView
{
	self.timeText.text = [NSString stringWithFormat:@"剩余时间：%ld" , (long)_leftTime];
	_leftTime--;
	// 时间小于0, 游戏失败
	if (_leftTime < 0)
	{
		[_timer invalidate];
		// 更改游戏的状态
		_isPlaying = NO;
		[_lostAlert show];
		return;
	}
}
- (void) alertView:(UIAlertView *)alertView
	clickedButtonAtIndex:(NSInteger)buttonIndex
{
	// 如果用户选中的“确定”按钮
	if(buttonIndex == 1)
	{
		[self startGame];
	}
}
- (void)checkWin:(FKGameView *)gameView
{
	// 判断是否还有剩下的方块, 如果没有, 游戏胜利
	if (![gameView.gameService hasPieces])
	{
		// 游戏胜利
		[_successAlert show];
		// 停止定时器
		[_timer invalidate];
		// 更改游戏状态
		_isPlaying = NO;
	}
}
@end
